<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Validation\Annotation\Scene;
use App\Service\User as UserService;

#[AutoController]
class UserController extends AbstractController
{
    // 用户列表
    public function list()
    {
        $map = [];
        $phone = $this->request->input('phone', '');
        if ($phone) {
            $map[] = ['phone', '=', $phone];
        }

        $username = $this->request->input('username', '');
        if ($username) {
            $map[] = ['username', '=', $username];
        }

        $status = $this->request->input('status', -1);
        if ($status > -1) {
            $map[] = ['status', '=', $status];
        }


        $result = (new UserService())->list($map);
        return $this->returnSuccess($result, '成功');
    }

    // 获取用户详细信息
    public function info()
    {
        $user_id = $this->request->input('user_id', 0);
        if (!$user_id)
            return $this->returnError('请求参数错误');
        $data = (new UserService())->detail($user_id);
        return $this->returnSuccess($data, '成功');
    }

    // 用户群体列表
    public function group_list()
    {
        $map = [];
        $result = (new UserService())->groupList($map);
        return $this->returnSuccess($result, '成功');
    }

    // 添加用户群体
    public function add_user_group()
    {
        $group_name = $this->request->input('group_name', '');
        $filter = $this->request->input('filter', '');
        if (!$group_name || !$filter)
            return $this->returnError('请求参数错误');
        $result = (new UserService())->addUserGroup(['group_name' => $group_name, 'filter' => $filter]);
        return $this->returnSuccess($result, '保存成功');
    }

    // 删除用户群体
    public function delete_user_group()
    {
        $id = $this->request->input('id', 0);
        if (!$id)
            return $this->returnError('请求参数错误');
        $data = (new UserService())->deleteUserGroup($id);
        return $this->returnSuccess($data, '删除成功');
    }
}
